﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Tsanie.ShellExt.Definitions;

namespace Tsanie.ShellExt
{
    public abstract class PropertySheetBaseProvider : BaseProvider
    {
        /// <summary>
        /// 获取属性页模版
        /// </summary>
        public abstract PropertySheetTemplate GetTemplate(string[] files);

        public virtual bool AllowMultiFiles { get { return false; } }

        public virtual void OnCreatePage() { }
        public virtual NotifyResult OnCancel() { return NotifyResult.NoError; }
        public virtual NotifyResult OnActivate() { return NotifyResult.NoError; }
        public virtual NotifyResult OnApply() { return NotifyResult.NoError; }
        public virtual NotifyResult OnShowHelp() { return NotifyResult.NoError; }
    }

    /// <summary>
    /// 属性页模版类
    /// </summary>
    [Designer("System.Windows.Forms.Design.DocumentDesigner, System.Design", typeof(IRootDesigner))]
    [DesignerCategory("Form")]
    public class PropertySheetTemplate : ScrollableControl
    {
        #region - Privates -

        bool m_IsXP = false;

        #endregion

        #region - API -

        const string VSCLASS_TAB = "TAB";

        const int TABP_TABITEM = 1;
        const int TABP_TABITEMLEFTEDGE = 2;
        const int TABP_TABITEMRIGHTEDGE = 3;
        const int TABP_TABITEMBOTHEDGE = 4;
        const int TABP_TOPTABITEM = 5;
        const int TABP_TOPTABITEMLEFTEDGE = 6;
        const int TABP_TOPTABITEMRIGHTEDGE = 7;
        const int TABP_TOPTABITEMBOTHEDGE = 8;
        const int TABP_PANE = 9;
        const int TABP_BODY = 10;
        const int TABP_AEROWIZARDBODY = 11;

        const int TIS_NORMAL = 1;

        const int TMT_FILLCOLOR = 3802;
        const int TMT_EDGELIGHTCOLOR = 3804;
        const int TMT_EDGEHIGHLIGHTCOLOR = 3805;
        const int TMT_EDGESHADOWCOLOR = 3806;
        const int TMT_EDGEDKSHADOWCOLOR = 3807;
        const int TMT_EDGEFILLCOLOR = 3808;
        const int TMT_GLOWCOLOR = 3816;
        const int TMT_FILLCOLORHINT = 3821;

        [DllImport("uxtheme.dll")]
        extern static bool IsThemeActive();
        [DllImport("uxtheme.dll")]
        extern static int GetThemeColor(
           IntPtr hTheme,
           int iPartId,
           int iStateId,
           int iPropId,
           [Out] out int pColor);
        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
        extern static IntPtr OpenThemeData(IntPtr hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszClassList);
        [DllImport("uxtheme.dll")]
        extern static IntPtr CloseThemeData(IntPtr hTheme);
        [DllImport("uxtheme.dll")]
        extern static int DrawThemeBackground(
           IntPtr hTheme,
           IntPtr hDc,
           int iPartId,
           int iStateId,
           ref Rectangle pRect,
           ref Rectangle pClipRect);

        #endregion

        public PropertySheetTemplate()
            : base()
        {
            //base.ResizeRedraw = true;
            base.Size = new Size(341, 408);
            //base.BackColor = Color.Transparent;
            //base.ForeColor = Color.FromKnownColor(KnownColor.WindowText);
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                if (Environment.OSVersion.Version.Major > 5 && Environment.OSVersion.Version.Minor > 0)
                {
                    m_IsXP = true;
#if DEBUGPROPERTY
                    Common.OutputDebugString("PropertySheetBase(void):: system is greater than XP.");
#endif
                    if (IsThemeActive())
                    {
                        IntPtr hTheme = OpenThemeData(this.Handle, VSCLASS_TAB);
                        int pColor;
                        GetThemeColor(hTheme, TABP_BODY, TIS_NORMAL, TMT_GLOWCOLOR, out pColor);
#if DEBUGPROPERTY
                        Common.OutputDebugString("pColor: #{0:x6}", pColor);
#endif
                        CloseThemeData(hTheme);
                        base.BackColor = ColorTranslator.FromWin32(pColor);
                    }
                }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (m_IsXP && IsThemeActive())
            {
#if DEBUGPROPERTY
                Common.OutputDebugString("OnPaintBackground(PaintEventArgs):: Draw XP visual styles.");
#endif
                IntPtr hTheme = OpenThemeData(this.Handle, VSCLASS_TAB);
                IntPtr hDC = e.Graphics.GetHdc();
                Rectangle rect = this.ClientRectangle;
                DrawThemeBackground(hTheme, hDC, TABP_BODY, TIS_NORMAL, ref rect, ref rect);
                e.Graphics.ReleaseHdc(hDC);
                CloseThemeData(hTheme);
            }
        }

        #region - Properties -

        [DefaultValue(typeof(Size), "341, 408")]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }
        //[DefaultValue(typeof(Color), "-1")]
        //public override Color BackColor {
        //    get { return base.BackColor; }
        //    set { base.BackColor = value; }
        //}
        [Category("Property Sheet")]
        public bool ShowHelpButton { get; set; }
        [Category("Property Sheet")]
        public Bitmap Icon { get; set; }

        #endregion

    }
}
